<template>
  <div class="contentcate">
    <el-card>
      <!-- 头部区域 -->
      <header-search>
        <template #search>
          <span>分类名称</span>
          <el-input
            v-model="queryData.catename"
            placeholder="请输入分类名称"
            style="width: 220px"
            size="small"
          />
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
            @click="searchList"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="btnReset">重置</el-button>
        </template>

        <template #addContent>
          <el-button
            icon="el-icon-plus"
            type="primary"
            size="small"
            @click.native="openDialog"
          >新增</el-button>
          <!-- 导出组件 -->
          <export-upload type="cate" />
        </template>
      </header-search>
      <!-- 列表 -->
      <el-table :data="tableData" border style="width: 100%" class="articleList">
        <el-table-column fixed label="#" width="80px" type="index" :index="indexMethod" />
        <el-table-column fixed prop="id" label="分类ID" />
        <el-table-column prop="catename" label="分类名称" />
        <el-table-column prop="icon" label="图标class" />
        <el-table-column prop="default_data" label="权重" />
        <el-table-column fixed="right" label="操作">
          <template #default="{ row }">
            <el-button icon="el-icon-edit" type="primary" size="small" @click="editArticle(row.id)" />
            <el-button icon="el-icon-delete" type="danger" size="small" @click="delArticle(row.id)" />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        :current-page="queryData.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <add-dialog ref="addDialog" @updata="initData" />
  </div>
</template>
<script>
import { getArticleCateApi, delArticleCateApi } from '@/api/contnet/cate'
import AddDialog from './components/Add-dialog.vue'
export default {
  name: 'ContentCate',
  components: {
    AddDialog
  },
  data() {
    return {
      // 接收初始化数据
      tableData: [],
      // 请求参数
      queryData: {
        catename: '',
        page: 1,
        limit: 10
      },
      // 总数
      total: null
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 列表初始化
    async initData() {
      const res = await getArticleCateApi(this.queryData)
      this.tableData = res.data.data
      this.total = res.data.count
    },
    // 处列表理序号
    indexMethod(index) {
      return (this.queryData.page - 1) * this.queryData.limit + index + 1
    },
    // 分页器 => 每页显示条数
    handleSizeChange(val) {
      this.queryData.limit = val
      this.queryData.page = 1
      this.initData()
    },
    // 分页器 => 当前页
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 新增打开dialog
    openDialog() {
      this.$refs.addDialog.showDialog = true
    },
    // 删除分类
    async delArticle(id) {
      await this.$confirm('确认要删除吗', '提示')
      try {
        await delArticleCateApi(id)
        // ! 删除边界处理
        if (this.tableData.length === 1 && this.queryData.page > 1) {
          this.queryData.page--
        }
        // 1. 成功提示
        this.$message.success('删除成功')
        // 2. 刷新列表
        this.initData()
      } catch (error) {
        return console.log(error)
      }
    },
    // 编辑分类
    editArticle(id) {
      this.$refs.addDialog.getArticleDetail(id)
    },
    // 搜索区域
    async searchList() {
      const res = await getArticleCateApi(this.queryData)
      this.tableData = res.data.data
      this.total = res.data.count
    },
    // 重置
    btnReset() {
      this.queryData.catename = ''
      this.initData()
    }
  }
}
</script>
<style lang="scss" scoped>
.contentcate {
  padding: 20px 20px 0 20px;
  .el-pagination {
    margin-top: 20px;
    float: right;
  }
  .articleList {
    .el-button {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      padding: 0 ;
    }
  }
}
</style>
